const http = require('http')
const querystring = require('querystring')
const { fstat } = require('fs')
const fs = require('fs')

let filename = './cat.jpg'
fs.stat(filename,(error,stat)=>{
    const options = {
        host: 'localhost',
        port: 3000,
        path: '/?filename=cat.jpg',
        headers: {
            'Content-Type': 'application/octet-stream',
            'Content-length': stat.size
        }
    }
    const req = http.request(options, (res)=> {
        console.log(`STATUS: ${res.statusCode}`)
        console.log(`HEADERS: ${JSON.stringify(res.headers)}`)
    })
    req.on('error',(e)=> {
        console.error(`problem with request: ${e.message}`)
    })
    let readStream = fs.createReadStream('./cat.jpg')
    readStream.pipe(req)
    readStream.on('end',()=> {
        req.end()
    })
})